package com.tuanzi.loan.business.service.family.impl;

import java.util.Objects;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.family.CommSysCode;
import com.tuanzi.loan.business.repository.family.CommSysCodeRepository;
import com.tuanzi.loan.business.service.family.CommSysCodeService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.SpaceConfInfoSearchRequest;
import com.tuanzi.loan.vo.response.family.SpaceConfInfoSearchResponse;
import com.tuanzi.loan.vo.view.family.SpaceConfInfoView;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class CommSysCodeServiceImpl implements CommSysCodeService {

	@Autowired
	private CommSysCodeRepository commSysCodeRepository;

	@Override
	public Page<SpaceConfInfoSearchResponse> list(PageSearcher<SpaceConfInfoSearchRequest> pageSearcher) {
		if (Objects.isNull(pageSearcher))
            throw new NullPointerException("pageSearcher is null");
        if (Objects.isNull(pageSearcher.getSearchCondition()) || Objects.isNull(pageSearcher.getPageable()))
            throw new NullPointerException("spaceConfInfoSearchRequest or pageable is null");
        return commSysCodeRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
	}
	
	@Override
	public SpaceConfInfoView loadSpaceConfInfoView(Long id) {
		SpaceConfInfoView request = new SpaceConfInfoView();
		CommSysCode commSysCode = commSysCodeRepository.findOne(id);
		BeanUtils.copyProperties(commSysCode, request);
		return request;
	}
	
	@Override
	public void saveOrUpdateSpaceConfInfoView(SpaceConfInfoView model) {
		CommSysCode commSysCode = null;
		if(model.getId() != null && model.getId() != 0) {
			commSysCode = commSysCodeRepository.findOne(model.getId());
		} else {
			commSysCode = new CommSysCode();
			commSysCode.setName("Advertise类型");
			commSysCode.setType("advertise");
		}
		BeanUtils.copyProperties(model, commSysCode);	
		commSysCodeRepository.save(commSysCode);
	}

	
}











